;define(function (require) {
	'use strict';
	var d3=require('d3');
	var $=require('jquery');
	var logs=require('plugin/logs');

	function timeline(id){
		this.id=id;
		this.w=0;
		this.count=0;
		this.items;
		this.opts={
			startyear:2015,
			startmonth:1,
			endyear:2015,
			endmonth:12,
			step:10,
			interval:1500,
			callback:null,
			prevbtn:null,
			nextbtn:null,
			idx:0
		};
	}
	timeline.prototype={
		init:function(opts){
			$.extend(true, this.opts, opts);
			var items=this.generateItems();
			this.generateDom(items);

			this.count=items.length;
			this.w=$(this.id).find('.timelinewarper').width();
			$(this.id).find('.timeline').width(this.count*50);
			this.items.eq(this.opts.idx).addClass('active');
		},
		generateItems:function(){
			var sy=this.opts.startyear,
					ey=this.opts.endyear,
					sm=this.opts.startmonth,
					em=this.opts.endmonth;
			var items=[];
			var idx=0;
			for(;sy<=ey;sy++){
				var i=0,l=12;
				if(sy==this.opts.startyear){
					i=this.opts.startmonth;
				}else if(sy==this.opts.endyear){
					l=this.opts.endmonth;
				}else{
					i=0,l=11;
				}
				var m;
				for(;i<l;i++){
					m=(i==0||i==this.opts.startmonth)?(sy+'年'):((i+1)+'月');
					items.push('<li data-idx="'+(idx++)+'" data-year="'+sy+'" data-month="'+(i+1)+'" class="item"><i><b></b></i>'+m+'</li>');
				}
			}
			return items;
		},
		generateDom:function(items){
			var domstr='<span class="prevbtn timebtn"><i class="fa fa-angle-left"></i></span>'
								+'<div class="timelinewarper">'
								+'	<ul class="timeline">'
								+items.join('')
								+'	</ul>'
								+'</div>'
								+'<span class="nextbtn timebtn"><i class="fa fa-angle-right"></i></span>';
			$(this.id).html(domstr);
			this.bindEvent();
		},
		bindEvent:function(){
			var that=this;
			$(this.id).find('.prevbtn').on('click',function(){
				if('function'===typeof that.opts.prevbtn) that.opts.prevbtn();
			});
			$(this.id).find('.nextbtn').on('click',function(){
				if('function'===typeof that.opts.nextbtn) that.opts.nextbtn();
			});
			this.items=$(this.id).find('.item');
			this.items.on('click',function(){
				that.items.eq(that.opts.idx).removeClass('active');
				$(this).addClass('active');
				that.opts.idx=$(this).data('idx');
				if('function'===typeof that.opts.callback) that.opts.callback.call(this);
			});
		},
		setMonth:function(i){
			if(i>=this.count){
				i=0;
			}
			that.items.eq(that.opts.idx).removeClass('active');
			that.items.eq(i).addClass('active');
			that.opts.idx=i;
			if('function'===typeof that.opts.callback) that.opts.callback.call(this);
		}
	};
	return timeline;
});